package logic

import (
	"context"
	"taobao/taobaorpc/taobaorpc"

	"taobao/api/internal/svc"
	"taobao/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type ProductAddLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewProductAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ProductAddLogic {
	return &ProductAddLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *ProductAddLogic) ProductAdd(req *types.ProductRequest) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line

	add, err := l.svcCtx.TaoBaoRpc.ProductAdd(l.ctx, &taobaorpc.ProductAddRequest{
		Title:  req.Title,
		UserId: req.UserId,
		Price:  float32(req.Price),
		Stock:  req.Stock,
		Image:  req.Image,
	})

	if err != nil {
		return &types.Response{
			Message: err.Error(),
			Data:    nil,
			Code:    500,
		}, nil
	}

	return &types.Response{
		Message: "添加商品成功",
		Data:    add,
		Code:    200,
	}, nil
}
